package com.sf.web.advice;

import com.sf.ex.BusinessException;
import com.sf.util.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 标记我们当前这个类是异常处理类
 * 处理异常
 */
@ControllerAdvice
public class CommonControllerAdvice {

    /**
     * 把要处理的异常放到 ExceptionHandler (异常类型.class) 注解
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public JsonResult handler(Exception e){
        e.printStackTrace();
        return new JsonResult(false,"操作失败",null);
    }

    // 自定义异常
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public JsonResult handler(BusinessException e){
        return new JsonResult(false,e.getMessage(),null);
    }



}
